package edu.hhu.controller;

import com.jfinal.core.Controller;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.auth.AccessTokenBuilder;

import edu.hhu.basic.table.CreateExcel;
import edu.hhu.dynamic.table.Conditions;
import edu.hhu.dynamic.table.QueryExcel;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PoiController extends Controller {

    public void createExcel() {
		Conditions conditions=new Conditions();

		String nodeStr=this.getPara("nodeStr");
		nodeStr=nodeStr.substring(0, nodeStr.length()-1);
		conditions.setNodeStr(nodeStr);
		
		String nodesNames=this.getPara("nodesNames");
		nodesNames=nodesNames.substring(0, nodesNames.length()-1);
		
		String fieldStr=this.getPara("fieldStr");
		fieldStr=fieldStr.substring(0, fieldStr.length()-1);
		conditions.setFieldStr(fieldStr);
		
		String s_date=this.getPara("s_date");
		conditions.setS_date(s_date);
		
		String e_date=this.getPara("e_date");
		conditions.setE_date(e_date);
		
		//真假表切换
		String tablename = this.getPara("tablename");
		conditions.setTablename(tablename);
		//tablename = "pcdtsjtrue";
		
		// 归一
		String level = this.getPara("level");
		conditions.setLevel(level);
		// 判断数据来源的table
		if (level.equals("3")) {
			conditions.setTablename("pcdtsjtrue");
		} else if (level.equals("2")) {
			conditions.setTablename("jzhz");
		} else if (level.equals("1")) {
			conditions.setTablename("qkhz");
		}else{
			conditions.setTablename("qthz");
		}
		String chartStyle = this.getPara("chartStyle");
		conditions.setChartStyle(chartStyle);
		String normalType = this.getPara("normalType");
		conditions.setNormalType(normalType);

		//获取系统物理位置
        String path=this.getRequest().getSession().getServletContext().getRealPath("/");
        String filePath="files/"+nodesNames.split(",")[0]+"等井数据.xls";

        QueryExcel excel = new QueryExcel();
		try {
			excel.writeExcel(path+filePath,conditions);
		} catch (Exception e) {
			e.printStackTrace();

		}
		renderJson("success");
    }

    public void getExcel() {
    	String nodesNames=this.getPara("nodesNames");
		try{
			nodesNames = URLDecoder.decode(nodesNames, "UTF-8");
		}catch(Exception e){
			e.printStackTrace();
		}
		nodesNames=nodesNames.substring(0, nodesNames.length()-1);

        renderFile("/files/"+nodesNames.split(",")[0]+"等井数据.xls");
    }

public void exportExcel() {
    	
		String table=this.getPara("table");		
		System.out.println(table);
        String str = getPara("id");
        System.out.println(str);
        String check= getPara("choice");
        String[] split = str.split(",");
        
        String path=this.getRequest().getSession().getServletContext().getRealPath("/");
        String filePath="files/"+table+".xls";
//		System.out.println(path);
//		System.out.println(filePath);
        

        CreateExcel excel = new CreateExcel();
		try {
			excel.writeExcel(path+filePath,table,split,check);
		} catch (Exception e) {
			e.printStackTrace();

		}
		renderJson("success");
    }


public void saveExcel() {
	String table=this.getPara("table");
	System.out.println(table);
//	System.out.println(nodeStr);

    renderFile("/files/"+table+".xls");
}
}
